// pages/myHome/index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
const db = wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    dynamiclist: [{
        num: 1,
        hint: '我看过的',
        url: '/pages/mySaw/index'
      },
      {
        num: 1,
        hint: '我发布的',
        url: '/pages/myDynamic/index'
      },
      {
        num: 1,
        hint: '我收藏的',
        url: '/pages/mylove/index'
      },
    ],
    toollist: [

      {
        name: '社区活动',
        img: '../../images/team.png',
        url:'/pages/notfound/index'
      },
      {
        name: '平台规则',
        img: '../../images/customer.png',
        url:'/pages/partformRules/index'
      },
      {
        name: '关于我们',
        img: '../../images/aboutus.png',
        url:'/pages/aboutus/index'
      },
      {
        name: '加入我们',
        img: '../../images/join.png',
        url:'/pages/joinus/index'

      },
    ]
  },
  gotargetpath(e) {
    wx.navigateTo({
      url: '/pages/editInfo/index'
    })
  },
  showtoast(e){
    wx.showToast({
      title: '即将上线',
      icon:'none'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = wx.getStorageSync('userInfo')
    const NICKNAME = wx.getStorageSync('NICKNAME')
const AVATARURL = wx.getStorageSync('AVATARURL')
if(!data){
  data = {}
  data.nickname = NICKNAME
  data.avatar = AVATARURL
}
    this.getNum()
    this.setData({
      userInfo: data
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  async getNum() {
    let dynamiclist = this.data.dynamiclist
    let sawtime = (wx.getStorageSync('SAWDYN') || []).length
    dynamiclist[0].num = sawtime
    wx.cloud.database().collection('dynamiclist').where({
      _openid: wx.getStorageSync('OPENID')
    }).count({
      success: (res) => {
        dynamiclist[1].num = res.total
        this.setData({
          dynamiclist: dynamiclist
        })
      }
    })
    wx.cloud.init({
      env: 'cloud1-5glocnmp62950062'
    })
    wx.cloud.callFunction({
      name: 'getDynamic',
      success: (res) => {
        console.log(res)
        wx.hideLoading({
          success: (res) => {},
        })
        let dynamicData = res.result.dynamiclist.list.reverse().filter(item => {
          item.like = item.love.length
          return item.love.length
        })
        dynamiclist[2].num = dynamicData.length
        this.setData({
          dynamiclist: dynamiclist
        })
        console.log(this.data.dynamiclist)
      },
      fail: function (err) {
        wx.showToast({
          title: '获取数据失败，请稍后再试',
          icon: 'none',
        })
        console.error(err)
      },
      complete: res => {}
    })

  },
  getUserinfo: function () {
    let that = this
    wx.cloud.init({
      env: 'cloud1-5glocnmp62950062'
    })
    wx.cloud.callFunction({
      name: 'getUserinfo',
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.result.curinfo.data[0]
        })
        wx.setStorageSync('userInfo', res.result.curinfo.data[0])
      },
      fail: function (err) {
        console.error(err)
      }
    })
  },
  goEditinfo: function () {
    wx.navigateTo({
      url: '/pages/editInfo/index',
    })
  },
  goItempage: function (e) {
    let url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
    })
  },
  onPullDownRefresh: function () {
    this.getUserinfo()
    this.getNum()
    wx.stopPullDownRefresh()
  }
})